

//
//  FJYPopView.m
//  PopView
//
//  Created by FJY on 16/1/6.
//  Copyright © 2016年 FJY. All rights reserved.
//

#import "FJYPopView.h"
#import "BaseHeader.h"

@implementation FJYPopView

{
    UIView *bgView;
    UILabel *title;
    UILabel *detail;
    UIButton *okButton;
    UIButton *cancelButton;
    
}

//singleTon
+ (FJYPopView *)sharedFJYPopView {
    static dispatch_once_t once;
    static FJYPopView *popView;
    dispatch_once(&once, ^{
        popView = [[FJYPopView alloc] init];
    });
    return popView;
}

+ (void)popViewShow:(UIView *)view title:(NSString *)title detail:(NSString *)detail okButton:(NSString *)ok cancelButton:(NSString *)cancel okButtonCall:(call)okCall cancel:(call)cancelCall {
    [UIView animateWithDuration:2 animations:^{
        
        [[FJYPopView sharedFJYPopView] bgViewSet:view];
        [[FJYPopView sharedFJYPopView] controlsSet];
        [[FJYPopView sharedFJYPopView] contentSet:title detail:detail okButton:ok cancelButton:cancel];
        [[FJYPopView sharedFJYPopView] setOkClickBlock:nil];
        [[FJYPopView sharedFJYPopView] setCancelClickBlock:nil];
        [[FJYPopView sharedFJYPopView] setOkClickBlock:okCall];
        [[FJYPopView sharedFJYPopView] setCancelClickBlock:cancelCall];
        
    }];
    
}


- (void)bgViewSet:(UIView *)view{
    if (bgView) {
        [bgView removeFromSuperview];
        bgView = nil;
    }
    
    bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth/1.15, kHeight/4)];
    bgView.center = CGPointMake(view.center.x, view.center.y - 40);
    bgView.backgroundColor = kCreamColor;
    bgView.layer.cornerRadius  = 20;
    bgView.layer.shadowColor = [[UIColor grayColor] CGColor];
    bgView.layer.shadowOffset = CGSizeMake(0, 5);
    bgView.layer.shadowOpacity = 0.3f;
    bgView.layer.shadowRadius = 10.0f;
    [view addSubview:bgView];
    [view bringSubviewToFront:bgView];
    
}


- (void)controlsSet {
    
    CGFloat w = bgView.frame.size.width;
    CGFloat h = bgView.frame.size.height;
    
    title = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, w, 18 + 4)];
    title.font = [UIFont boldSystemFontOfSize:18];
    title.textAlignment  = NSTextAlignmentCenter;
    
    detail = [[UILabel alloc] initWithFrame:CGRectMake(0, title.frame.origin.y + title.frame.size.height + title.frame.size.height/2, w - 30 , h/3)];
    detail.numberOfLines = 0;
    detail.center = CGPointMake(title.center.x, detail.center.y);
    detail.font = [UIFont systemFontOfSize:16];
    detail.layer.cornerRadius = 20;
    detail.textAlignment = NSTextAlignmentCenter;
    
    okButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    okButton.layer.cornerRadius = 5;
    okButton.titleLabel.font = [UIFont systemFontOfSize:18];
    okButton.center = CGPointMake(detail.center.x - detail.frame.size.width/4, detail.center.y + detail.frame.size.height/2 + 20);
    okButton.bounds = CGRectMake(0, 0, (w - 50) / 2, 16 + 4);
    okButton.tag = 1000;
    okButton.hidden = YES;
    
    cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    cancelButton.layer.cornerRadius = 5;
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:18];
    cancelButton.center = CGPointMake(detail.center.x + detail.frame.size.width/4, detail.center.y + detail.frame.size.height/2 + 20);
    cancelButton.bounds = CGRectMake(0, 0, (w - 50) / 2, 16 + 4);
    cancelButton.tag = 1001;
    cancelButton.hidden = YES;
    
    [bgView addSubview:title];
    [bgView addSubview:detail];
    [bgView addSubview:okButton];
    [bgView addSubview:cancelButton];
    [okButton addTarget:self action:@selector(okButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
}

- (void)contentSet:(NSString *)titleText detail:(NSString *)detailText okButton:(NSString *)ok cancelButton:(NSString *)cancel {
    
    title.text = titleText;
    detail.text = detailText;
    detail.frame = CGRectMake(detail.frame.origin.x, detail.frame.origin.y, detail.frame.size.width, [FJYPopView heightForLabel:detail]);
    cancelButton.center = CGPointMake(detail.center.x + detail.frame.size.width/4, detail.center.y + detail.frame.size.height/2 + 20);
    okButton.center = CGPointMake(detail.center.x - detail.frame.size.width/4, detail.center.y + detail.frame.size.height/2 + 20);
    bgView.frame = CGRectMake(0, 0, bgView.frame.size.width, cancelButton.frame.origin.y + cancelButton.frame.size.height + 15);
    bgView.center = CGPointMake(kWidth/2, kHeight/2 - 100);
    
    BOOL hasOneButton = NO;
    //Button number judge(only one button)
    if (cancel == nil && ok != nil) {
        hasOneButton = YES;
    }
    okButton.hidden = NO;
    [okButton setTitle:ok forState:(UIControlStateNormal)];
    [okButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    if (hasOneButton) {
        okButton.center = CGPointMake(detail.center.x, okButton.center.y);
        okButton.bounds = CGRectMake(0, 0, okButton.frame.size.width, okButton.frame.size.height);
        cancelButton.hidden = YES;
    } else {
        cancelButton.hidden = NO;
        [cancelButton setTitle:cancel forState:(UIControlStateNormal)];
        [cancelButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }
    
    
}

- (void)okButtonAction:(UIButton *)click {
    self.okClickBlock(click.tag - 1000);
}

- (void)cancelButtonAction:(UIButton *)click {
    self.cancelClickBlock(click.tag - 1000);
}

- (void)setBgViewColor:(UIColor *)color {
    bgView.backgroundColor = color;
}

+ (CGFloat)heightForLabel:(UILabel *)label {
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    CGRect bounds = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, 1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dictionary context:nil];
    CGFloat labelHeight = bounds.size.height;
    return labelHeight > label.frame.size.height ? labelHeight : label.frame.size.height;
}

- (void)popViewDisappear {
    if (bgView) {
        [UIView animateWithDuration:1 animations:^{
            for (UIView *view in bgView.subviews) {
                view.alpha = 0;
            }
            bgView.transform = CGAffineTransformMakeRotation(M_PI);
            bgView.transform = CGAffineTransformMakeTranslation(bgView.frame.origin.x/3, bgView.frame.origin.y/2);
            bgView.alpha = 0;
        } completion:^(BOOL finished) {
            for (UIView *view in bgView.subviews) {
                [view removeFromSuperview];
            }
            [bgView removeFromSuperview];
        }];
    }
}


@end
